// Copyright (c) 2024 by Rockchip Electronics Co., Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#ifndef _RKNN_DEMO_YOLO11_H_
#define _RKNN_DEMO_YOLO11_H_
#include "sms_core.h"
#include <string>
#include <chrono>
#include <mutex>
#include <queue>

#include <iostream>
#include <opencv2/opencv.hpp>
#include "rknn_api.h"
#include "common.h"

#if defined(RV1106_1103) 
    typedef struct {
        char *dma_buf_virt_addr;
        int dma_buf_fd;
        int size;
    }rknn_dma_buf;
#endif

typedef struct {
    rknn_context rknn_ctx;
    rknn_input_output_num io_num;
    rknn_tensor_attr* input_attrs;
    rknn_tensor_attr* output_attrs;
#if defined(RV1106_1103) 
    rknn_tensor_mem* input_mems[1];
    rknn_tensor_mem* output_mems[9];
    rknn_dma_buf img_dma_buf;
#endif
#if defined(ZERO_COPY)  
    rknn_tensor_mem* input_mems[1];
    rknn_tensor_mem* output_mems[9];
    rknn_tensor_attr* input_native_attrs;
    rknn_tensor_attr* output_native_attrs;
#endif
    int model_channel;
    int model_width;
    int model_height;
    bool is_quant;
} rknn_app_context_t;
#include "postprocess.h"

namespace sv2
{
  class rknnYolov11
  {
  public:
    rknnYolov11()
    {
      ;
    }
      ~rknnYolov11()
    {
      if(app_ctx)
      {
        delete app_ctx;
      }
    }
    //!用于初始化yolov11，仅调用一次
    /*!
    \param model_path:
    \param size:分辨率，如cv::Size(1920，1080)
    \param confidence:
    \param nms_thresh:
    返回是否初始化成功
    */
    bool setup(std::string model_path, float confidence, float nms_thresh,  nlohmann::json g_dataset_categories,  nlohmann::json dataset_name);
    //!判断yolov11是否已经打开
    bool isOpened();

    //!用于每一帧图像，在循环中被调用
    /*!
    \param image:
    */
    void stream(cv::Mat image, nlohmann::json tgts_json);
    //!释放资源
    void release();

    void trans_det_results(object_detect_result_list results, int w, int h, std::vector<int> roi, nlohmann::json& msg);
    void getconfig(sms::Publisher* _result_writer, sms::Publisher* _show_writer, sms::Publisher* _next_writer, bool b_use_shm, bool launch_next_emit);
    void pushmsg(nlohmann::json msg, nlohmann::json msg_results, cv::Mat image);

    std::string model_path;
    float confidence; 
    float nms_thresh;
    cv::Size size;
    rknn_app_context_t* app_ctx;
    bool launch_next_emit;
    bool b_use_shm;

    nlohmann::json _g_dataset_categories;
    nlohmann::json _dataset_name;

    sms::Publisher* _result_writer;
    sms::Publisher* _show_writer;
    sms::Publisher* _next_writer;
  };
}
#endif //_RKNN_DEMO_YOLO11_H_